package liuch.foreach;

import com.sun.xml.internal.ws.server.UnsupportedMediaException;

import java.lang.reflect.Array;
import java.util.*;

/**
 * 〈一句话功能简述〉<br>
 *  继承IterableClass实现正序，反序
 *
 * @author 17101651 on 2018/5/18.
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class MulitItterableClass extends IterableClass {

    /**
     * 反向排列
     *
     * @return
     */
    public Iterable<String> reversed() {
        return new Iterable<String>() {
            @Override
            public Iterator<String> iterator() {
                return new Iterator<String>() {
                    int curt = words.length - 1;

                    @Override
                    public boolean hasNext() {
                        return curt > -1;
                    }

                    @Override
                    public String next() {
                        return words[curt--];
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedMediaException();
                    }
                };
            }
        };
    }

    /**
     * 乱序
     *
     * @return
     */
    public Iterable<String> randomized() {
        return new Iterable<String>() {
            @Override
            public Iterator<String> iterator() {
                List<String> shuffled = new ArrayList<>(Arrays.asList(words));
                Collections.shuffle(shuffled, new Random(47));
                return shuffled.iterator();
            }
        };
    }

    public static void main(String[] args) {
        MulitItterableClass mulitItterableClass = new MulitItterableClass();
        for (String s : mulitItterableClass.reversed()) {
            System.out.print(s + "  ");
        }
        System.out.println();
        for (String s : mulitItterableClass.randomized()) {
            System.out.print(s + "  ");
        }
        System.out.println();
        for (String s : mulitItterableClass) {
            System.out.print(s + "  ");
        }
    }
}
